<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Support\Facades\Request;
use Modules\Admin\Service\GradeService;
use Modules\Admin\Service\CouponService;
use Modules\Admin\Http\Requests\Grade\GradeRequest;


class GradeController extends BaseController
{

    /**
     *  会员等级列表
     *  hinq 2019 08 21
     *
     **/
    public function index()
    {
        $arr_search = (array)Request::input();
        $arr_list = GradeService::get_list((array)$arr_search);
        echo_error($arr_list);
        return view('admin::grade.index', $arr_list['data']);
    }

    /**
     *  添加会员等级视图
     *  hinq 2019 08 21
     *
     **/
    public function create()
    {
        return view('admin::grade.create');
    }

    /**
     *  添加会员等级到数据库
     *  hinq 2019 08 21
     *
     **/
    public function store()
    {
        $arrInput = (array)Request::all();
        return GradeService::add_one((array)$arrInput);
    }


    /**
     *  编辑会员等级视图
     *  hinq 2019 08 21
     *
     **/
    public function edit()
    {
        $id = (int)Request::get('id', 0);
        $arr_info = GradeService::get_one((int)$id);
        return view('admin::grade.edit', ['info' => $arr_info]);
    }


    /**
     *  编辑会员等级数据
     *  hinq 2019 08 21
     *
     **/
    public function update()
    {
        $arrInput = (array)(array)Request::all();;
        $ret = GradeService::edit_one((array)$arrInput);
        return $ret;
    }

    /**
     *  设置会员等级的权益
     *  hinq 2019 08 28
     *
     **/
    public function update_eqs()
    {
        $arrInput = (array)Request::all();
        $ret = GradeService::edit_one((array)$arrInput);
        return $ret;
    }


    /**
     *  删除指定的数据
     *  hinq 2019 08 22
     *
     **/
    public function del()
    {
        $nId = (int)Request::get('id');
        return GradeService::delete_one((int)$nId);

    }

    /**
     *  删除指定的权益数据
     *  hinq 2019 08 23
     *
     **/
    public function del_equities()
    {
        $nId = (int)Request::get('id');
        return GradeService::delete_one_equities((int)$nId);
    }


    /**
     *  权益列表
     *  hinq 2019 08 22
     *
     **/
    public function index_equities()
    {
        $arr_search = (array)Request::input();
        $arr_list = GradeService::get_list_equities((array)$arr_search);
        echo_error($arr_list);
        return view('admin::grade.index_equities', $arr_list['data']);
    }


    /**
     *  添加权益视图
     *  hinq 2019 08 22
     *
     **/
    public function create_equities()
    {
        return view('admin::grade.create_equities');
    }

    /**
     *  添加权益数据
     *  hinq 2019 08 23
     *
     **/
    public function store_equities()
    {
        $arrInput = (array)Request::all();
        return GradeService::add_one_equities((array)array_filter($arrInput));
    }


    /**
     *  编辑权益视图
     *  hinq 2019 08 23
     *
     **/
    public function edit_equities()
    {

        $id = (int)Request::get('id', 0);
        $arr_info = GradeService::getOneEquities((int)$id);
        return view('admin::grade.edit_equities', ['info' => $arr_info]);
    }


    /**
     *  编辑权益数据
     *  hinq 2019 08 23
     *
     **/
    public function update_equities()
    {

        $arrInput = (array)Request::all();
        $ret = GradeService::edit_one_equities((array)array_filter($arrInput));
        return $ret;
    }


    /**
     *  添加权益礼包
     *  hinq 2019 08 23
     *
     **/
    public function create_gifts()
    {
        $nId = (int)Request::get('id');
        //当前权益信息
        $arr_info = GradeService::getOneEquities((int)$nId);
        //获取当前用户添加的优惠券( 这里只能平台设置 ，所有只能获取平台添加的优惠券)
        $arrWhere['where'] = ['admin_id' => admin_user_id(), 'status' => 1];
        $arrWhere['field'] = ['id', 'name'];
        $coupon_list = CouponService::get_where_list((array)$arrWhere);
        //当前权益下是否设置礼包
        $tempWhere['where'] = ['link_id' => $nId];
        $arr_list = GradeService::get_where_list_fo_gifts((array)$tempWhere);
        return view('admin::grade.create_gifts', ['info' => $arr_info, 'coupon_list' => $coupon_list, 'arr_list' => $arr_list]);
    }


    /**
     *  添加权益礼包数据
     *  hinq 2019 08 23
     *
     **/
    public function store_gifts()
    {
        $arrInput = Request::all();
        return GradeService::add_one_gifts((array)$arrInput);
    }


    /**
     *  删除权益礼包数据
     *  hinq 2019 08 23
     *
     **/
    public function del_gifts()
    {
        $nId = request::get('id');
        return GradeService::del_one((int)$nId);
    }


    /**
     *  编辑权益礼包数据
     *  hinq 2019 08 27
     *
     **/
    public function update_gifts()
    {
        $arrInput = Request::all();
        return GradeService::edit_one_gifts((array)$arrInput);
    }


    /**
     *  设置会员等级的权益
     *  hinq 2019 08 23
     *
     **/
    public function set_equities()
    {
        $nId = (int)Request::get('id');
        //获取当前会员等级信息 
        $arr_info = GradeService::get_one((int)$nId);
        //获取所有的权益列表
        $arr_list = GradeService::get_all();
        foreach ($arr_list as $key => $value) {
            if (!empty($arr_info['equities']) && in_array($value['id'], $arr_info['equities'])) {
                $arr_list[$key]['checked'] = true;
            } else {
                $arr_list[$key]['checked'] = false;
            }
        }
        return view('admin::grade.set_equities', ['info' => $arr_info, 'arr_list' => $arr_list]);

    }
}